<?php 
/*
		_____ _______ _____    ____   _______ _     _  _____
	   / ____|__   __|  __ \  / __ \ |__   __| |   | |/ ____|
      | (___ 	| |  | |__) || |__| |   | |  | |   | | (___ 
	 __\____\___|_|__|_____/_|______|___|_|__|_|___| |\____\____
	|_________________USER_FRIENDLY_CLOUD_OS____________________|
       ____) |	| |  | | \ \ | |  | |   | |  | |___| |____) |
      |_____/   |_|  |_|  \_\|_|  |_|	|_|   \_____/|_____/   VER: 1.0
 					
					 Stratus Operating System
*/

/**
 * Stratus.os
 * 
 * @name stratus
 * @copyright Stratus 2009
 * 
 * @description contains all neccesary functions need to run the os
 */
function isServiceLoaded($name){
	if(!name){
		return FALSE;
	}
	
	if(!defined(strtoupper($name). '_LOADED')){
		return FALSE; 
	} else {
		return TRUE;
	}
}

function loadService($name){
	if(!$name){
		return FALSE;
	}
	if(is_array($name)){
	 	foreach($name as $serv){
 			if(is_readable(STRATUS_PATH . '/' . STRATUS_OS . '/' . CORE_PATH . '/' . $serv . '/' . $serv . SERVICE_FILE)){
				include_once(STRATUS_PATH . '/' . STRATUS_OS . '/' . CORE_PATH . '/' . $serv . '/' . $serv . SERVICE_FILE);
				define(strtoupper($serv) . '_LOADED' , TRUE);
				if(function_exists($serv . '_init')){
					call_user_func($serv . '_init');
					Session::createSession(array('Services', true, $serv));
				} else {
					//echo '<div class="sys-error" style="color: #F08080">Unable to start service <b>' . $serv . '</b>. Errno: 001 Could not find service start routine</div>';	
					Session::createSession(array('Services', false, $serv));
				}
			} else {
				echo '<div class="sys-error" style="color: #F08080">could not load <b>' . $serv . '</b> service</div>';
			}
	 	}	
	 	return true;
	}
	
	if(is_readable(STRATUS_PATH . '/' . STRATUS_OS . '/' . CORE_PATH . '/' . $name . '/' . $name . SERVICE_FILE)){
		include_once(STRATUS_PATH . '/' . STRATUS_OS . '/' . CORE_PATH . '/' . $name . '/' . $name . SERVICE_FILE);
		define(strtoupper($name) . '_LOADED' , TRUE);
		if(function_exists($name . '_init')){
				call_user_func($name . '_init');
				Session::createSession(array('Services', true, $name));
			} else {
				//echo '<div class="sys-error" style="color: #F08080">Unable to start service <b>' . $name . '</b>. Errno: 001 Could not find service start routine</div>';			  
				Session::createSession(array('Services', false, $name));
			}
		return TRUE;
	} else {
		echo '<div class="sys-error" style="color: #F08080">could not load <b>' . $serv . '</b> service</div>';
	}
}

function loadLibrary($name){
	if(!$name){
		echo 'Improper parameter set!';
	}
	
	if(is_array($name)){
		foreach($name as $lib){
			if(is_readable(STRATUS_PATH . '/' . STRATUS_OS . '/' . CORE_PATH . '/' . $lib . LIB_FILE)){
				include_once(STRATUS_PATH . '/' . STRATUS_OS . '/' . CORE_PATH . '/' . $lib . LIB_FILE);
				define('LIB_' . strtoupper($lib) . '_LOADED', TRUE);
			} else {
				echo '<div class="sys-error" style="color: #F08080">could not load <b>' . $lib . '</b> service</div>';
			}
		}
	}
	
	if(is_readable(STRATUS_PATH . '/' . STRATUS_OS . '/' . CORE_PATH . '/' . $name . LIB_FILE)){
		include_once(STRATUS_PATH . '/' . STRATUS_OS . '/' . CORE_PATH . '/' . $name . LIB_FILE);
		define('LIB_' . strtoupper($name) . '_LOADED', TRUE);
	} else {
		echo '<div class="sys-error" style="color: #F08080">could not load <b>' . $name . '</b> service</div>';
	}
}

function listLoadedServices(){
	$services = Session::readSession('Services');
	foreach($services as  $name => $val){
		if($val == ''){
			echo '<li>'.ucfirst($name).' | Stopped</li>';
		} else {
			echo '<li>'.ucfirst($name).' | Running </li>';
		}
	}
}

function isLibraryLoaded($name){
	if(!$name){
		return FALSE;
	}
	
	if(!defined('LIB_' . strtoupper($name) . '_LOADED')){
		return FALSE;
	} else {
		return TRUE;
	}
}

function loadDotWeb(){
	if(is_readable(STRATUS_PATH . '/' . STRATUS_OS . '/' . CORE_PATH . '/' . 'dotweb' . LIB_FILE)){
		require_once(STRATUS_PATH . '/' . STRATUS_OS . '/' . CORE_PATH . '/' . 'dotweb' . LIB_FILE);
	} else { 
		exit('Error: [DOT]WEB Framework missing or corrupt. Please replace and try again.');
	}
}

// start os 
 loadDotWeb();
 
 loadService(array('event', 'task', 'viewer'));
 

	